#include "server.hpp"
#include "link.hpp"

rudpLink :: rudpLink( rudpServer * svr , const struct sockaddr * ad )
{
	p_svr = svr;
	add = ad;
	if( ad == nullptr ) throw ERR_PEER_NULL;
	if( svr == NULL ) throw ERR_SERVER_NULL;
}

rudpLink :: ~rudpLink()
{
	close();
}

size_t rudpLink :: send( const char * data , size_t len )
{
	if( data == nullptr ) return 0;
	if( p_svr == nullptr ) return 0;
	
	p_svr->send( data , len , add );
	
	return len;
}

void rudpLink :: close()
{
	if( p_svr ){
		p_svr->close( m_id );
	}
}

void rudpLink :: on_recv( std::function< void ( const char * data , size_t l ) > fun )
{
	recv_cb = fun;
}

void rudpLink :: on_send( std::function< void () >  fun )
{
	send_cb = fun;
}

void rudpLink :: on_close( std::function< void () > fun )
{
	close_cb = fun;
}

void rudpLink :: on_error( std::function< void ( err_code ) > fun )
{
	error_cb = fun;
}